package dth.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import dth.dao.BookMovieDAO;
import dth.entity.Account;
import dth.entity.BookMovie;
import dth.entity.OrderSetupBox;
import dth.entity.UserPackage;
import dth.utils.DthSessionConstant;
import dth.utils.DthUtil;

@ManagedBean(name = "ordermoviebean")
public class BookMovieBean {
	
	@ManagedProperty(value = "#{loginBean}")
	private LoginBean loginBean;// get session login bean
	
	private int id;
	private Date movieTime;
	private String priceStr;
	private String nameMovie;
	private Date date; 
	private Date minDate;
	private int status;
	private List<BookMovie> listBookMovies = new ArrayList<BookMovie>();
	
	public List<BookMovie> getListBookMovies() {
		System.out.println("----status movie:"+status);
		BookMovieDAO dao = new BookMovieDAO();
		if(status==2)
		{
			return dao.getAllListBookMovie();
		}
		else if(status == 0){
			return dao.getListBookMovie3();
		}
		else if(status==1){
			return dao.getListBookMovie4();
		}
		
		return listBookMovies;
	}

	public void setListBookMovies(List<BookMovie> listBookMovies) {
		this.listBookMovies = listBookMovies;
	}

	public int getStatus() {
		Object object=DthUtil.getSession(DthSessionConstant.DTH_status_bookmovie_id);
		if(object!=null){
			System.out.println("----getStatus id:"+object.toString());
			status=Integer.parseInt(object.toString());
		}
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setMinDate(Date minDate) {
		this.minDate = minDate;
	}



	public Date getMinDate() {
		java.util.Date date=new java.util.Date();//lay ngay hien tai
		return new Date(date.getTime());
	}

	

	public LoginBean getLoginBean() {
		return loginBean;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNameMovie() {
		return nameMovie;
	}

	public void setNameMovie(String nameMovie) {
		this.nameMovie = nameMovie;
	}

	public String getPriceStr() {
		return priceStr;
	}

	public void setPriceStr(String priceStr) {
		this.priceStr = priceStr;
	}

	public Date getMovieTime() {
		return movieTime;
	}

	public void setMovieTime(Date movieTime) {
		this.movieTime = movieTime;
	}

	
	
	
	public void setBookMovieBean()
	{
		System.out.println("--BookMovieBean");
		Object objectAction=DthUtil.getRequest().getParameter("action");
		System.out.println("----------action nnnnnnnnnnnnnnn:"+objectAction);
		if(objectAction!=null)
		{
			if(objectAction.equals("book"))
			{
				this.id = Integer.parseInt(DthUtil.getRequest().getParameter("movieId"));
				this.priceStr = DthUtil.getFormatedNumber(Double.parseDouble(DthUtil.getRequest().getParameter("moviePrice")));
				this.nameMovie = DthUtil.getRequest().getParameter("movieName");
				System.out.println("id = "+id +" price = " +priceStr+" Name= "+ nameMovie);
			}
					
		}
	}
	public BookMovieBean() {
		
		System.out.println("---request url:"+DthUtil.getRequestURL());
		if(DthUtil.getRequestURL().contains("bookmovie_history.xhtml")){
			System.out.println("--vao day role:"+LoginBean.roleCode);
			if(LoginBean.roleCode!=1)
				DthUtil.navigateTo("welcome.xhtml");
		}
		// TODO Auto-generated constructor stub
		System.out.println("--BookMovieBean");
		Object objectAction=DthUtil.getRequest().getParameter("action");
		System.out.println("----------action nnnnnnnnnnnnnnn:"+objectAction +movieTime);
		if(objectAction!=null)
		{
			if(objectAction.equals("book"))
			{
				this.id = Integer.parseInt(DthUtil.getRequest().getParameter("movieId"));
				this.priceStr = DthUtil.getFormatedNumber(Double.parseDouble(DthUtil.getRequest().getParameter("moviePrice")));
				this.nameMovie = DthUtil.getRequest().getParameter("movieName");
				System.out.println("id = "+id +" price = " +priceStr+" Name= "+ nameMovie);
			}
			if(objectAction.equals("delete"))
			{
				String temp = DthUtil.getRequest().getParameter("id");
				id = Integer.parseInt(temp);
				System.out.println("Delete book movie id: "+id);
			}
		}
	}
	
	public List<BookMovie> getListBookMovie() // su dung ben trang movie home
	{
		Account ac = DthUtil.getAccountDTH();
		if(ac!=null)
		{
			BookMovieDAO b = new BookMovieDAO();
			return b.getListBookMovie(ac.getFkUserId());
		}
		else
		{
			return null;
		}
	}
	
	public List<BookMovie> getAllListBookMovie()
	{
		Account ac = DthUtil.getAccountDTH();
		if(ac!=null)
		{
			BookMovieDAO b = new BookMovieDAO();
			return b.getAllListBookMovie();
		}
		else
		{
			return null;
		}
	}
	
	public void insert() {
		
//		String movieName = DthUtil.getRequest().getParameter("movieName");
//		int movieId  =  Integer.parseInt(DthUtil.getRequest().getParameter("movieId"));
		BookMovieDAO book = new BookMovieDAO(); 
		
		
		if(movieTime!=null)
		{
			System.out.println(nameMovie +"id "+id +" Timeeeeeeee "+ movieTime );
			
		
			BookMovie m = new BookMovie();
			m.setFkMovieId(id);
			
			m.setId(0); // khong wan tam, id tu. tang khi luu vao csdl
			java.util.Date date=new java.util.Date();//lay ngay hien tai
			m.setDateInsert(new java.sql.Date(date.getTime()));
			//m.setQuantity(quantity);
			m.setMovieTime( movieTime);
			Account ac = DthUtil.getAccountDTH();
			m.setFkUserId(ac.getFkUserId());////////////////
			int count = book.intsert(m);
			if(count ==1 )
				DthUtil.showMessage("Thank", "book successfully!");
			else
			{
				
				System.out.println("Bạn đã đặt film này trùng ngay");
				DthUtil.showMessage("Thông báo", " Bạn đã chọn film này trong ngày "+ movieTime.getDate()+" hãy chọn ngày khác.");
			}
		}
		else {
			System.out.println("Bạn chưa chọn ngày");
			DthUtil.showMessage("Error", "Bạn chưa chọn ngày");
		}
	}
	
	public void  delete()
	{
	
		System.out.println("id= "+id);
		BookMovieDAO dao = new BookMovieDAO();
		System.out.println("delete id: " +id);
		int count = dao.delete(id);
		if(count==1)
			DthUtil.showMessage("OK", "Xóa thành công!");
	}
	
	public void changeUserChooser(){
		System.out.println("---user id:"+status);
		DthUtil.setSession(DthSessionConstant.DTH_status_bookmovie_id, status);
	}
}
